<template>
    <el-tabs
        v-model="queryList.orgMenuType"
        type="card"
        class="demo-tabs"
        @tab-change="handleClick"
    >
        <el-tab-pane label="采购单位" :name="1"></el-tab-pane>
        <el-tab-pane label="门店" :name="2"></el-tab-pane>
        <el-tab-pane label="供应商" :name="3"></el-tab-pane>
        <el-tab-pane label="分销商" :name="4"></el-tab-pane>
    </el-tabs>
    <el-container>
        <el-header>
            <div class="left-panel">
                <el-button
                    type="primary"
                    icon="el-icon-plus"
                    :disabled="!addState"
                    @click="opAdd"
                >新增</el-button>
                <!-- <el-button
                    type="info"
                    plain
                    icon="el-icon-delete"
                    :disabled="!deleteState"
                    @click="opBatchDelete"
                >删除</el-button> -->
            </div>
            <div class="right-panel">
                <el-button :icon="Search"   type="primary" @click="opBindData">查询</el-button>
                <el-button :icon="Search" type="info" plain @click="resetSearch">重置</el-button>
                <el-button text type="primary" @click="showCondition = !showCondition">
                    <span v-if="!showCondition">展开</span>
                    <span v-else>收起</span>
                    <vab-icon
                        class="vab-dropdown"
                        :class="{ 'vab-dropdown-active': fold }"
                        icon="arrow-up-s-line"
                    />
                </el-button>
            </div>
        </el-header>

        <el-foot v-show="showCondition">
            <vab-query-form-top-panel>
                <el-form inline label-width="49px" :model="queryForm" style="margin-top: 5px">
                    <el-form-item label="菜单编码" label-width="100px">
                        <el-input
                            placeholder="---请输入菜单编码---"
                            v-model="queryList.menuCode"
                            style="width: 300px"
                        />
                    </el-form-item>
                    <el-form-item label="菜单名称" label-width="100px">
                        <el-input
                            placeholder="---请输入菜单名称---"
                            v-model="queryList.menuName"
                            style="width: 300px"
                        />
                    </el-form-item>
                    <el-form-item label="状态" label-width="100px">
                        <el-select
                            placeholder="---请选择状态---"
                            v-model="queryList.status"
                            style="width: 300px"
                        >
                            <el-option label="---请选择---" value />
                            <el-option
                                v-for="item in optionsState"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            />
                        </el-select>
                    </el-form-item>
                </el-form>
            </vab-query-form-top-panel>
        </el-foot>
        <el-main class="nopadding">
            <el-table
                :data="menuTree"
                style="width: 100%; margin-bottom: 20px"
                row-key="id"
                border
                default-expand-all
                :row-class-name="tableRowClassName"
            >
                <el-table-column type="selection" width="50"></el-table-column>
                <el-table-column label="菜单编码" prop="menuCode" width="180" sortable></el-table-column>
                <el-table-column label="菜单名称" prop="menuName" width="180" sortable></el-table-column>
                <el-table-column label="菜单路径" prop="menuPath" width="240" sortable></el-table-column>
                <el-table-column label="路由路径" prop="routePath" width="240" sortable></el-table-column>
                <el-table-column label="菜单图标" prop="menuIcon" width="240" sortable></el-table-column>
                <el-table-column label="菜单类型" prop="menuTypeDesc" width="180" sortable></el-table-column>
                <el-table-column label="排序序号" prop="sort" width="180" sortable></el-table-column>
                <el-table-column label="状态" fixed="right" align="center" width="120" sortable>
                    <template #default="scope">
                        <el-switch
                            v-model="scope.row.status"
                            :active-value="1"
                            :inactive-value="0"
                            @change="setStatus(scope.row)"
                        />
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" align="right" width="280">
                    <template #default="scope">
                        <el-button-group>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                @click="opShow(scope.row, scope.$index)"
                            >查看</el-button>
                            <el-button
                                text
                                type="primary"
                                size="small"
                                :disabled="!editState"
                                @click="opEdit(scope.row, scope.$index)"
                            >编辑</el-button>
                            <!-- <el-popconfirm
                                title="确定删除吗？"
                                @confirm="opDelete(scope.row, scope.$index)"
                            >
                                <template #reference>
                                    <el-button
                                        text
                                        type="primary"
                                        size="small"
                                        :disabled="!deleteState"
                                    >删除</el-button>
                                </template>
                            </el-popconfirm> -->
                        </el-button-group>
                    </template>
                </el-table-column>
            </el-table>
        </el-main>
    </el-container>

    <save-dialog
        v-if="dialogSave"
        ref="saveDialog"
        @fatherMethod="opBindData"
        @success="handleSaveSuccess"
        @closed="dialogSave = false"
    ></save-dialog>
</template>

<script>
import saveDialog from "./save";
import publicjs from "@/api/model/publicjs";
import tool from "@/utils/tool";
import config from "@/config";
export default {
    name: "menuList",
    components: {
        saveDialog,
        publicjs,
    },
    data() {
        return {
            dialogSave: false,
            showCondition: false,
            selectionRows: [],
            pkList: ["menuId"],
            columnList: [],
            OrderList: { menuId: "Asc" },
            queryList: {
                orgMenuType: 1,
            },
            result: "", // 运行结果
            props1: {
                checkStrictly: true,
                emitPath: false,
            },
            addState: true, //可以增加
            editState: true, //可以修改
            editBatchState: true, //可以批量修改
            deleteState: true, //可以删除
            optionsState: [],
            menuTree:[]
        };
    },
    methods: {
        //添加
        opAdd() {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Add").setData({
                    menuCode: "",
                    menuName: "",
                    menuPath: "",
                    routePath: "",
                    menuIcon: "",
                    parentId: 0,
                    menuType: 0,
                    orgType: this.queryList.orgMenuType,
                    status: 1,
                });
            });
        },
        //编辑
        opEdit(row) {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Edit").setData(row);
            });
        },
        //查看
        opShow(row) {
            this.dialogSave = true;
            this.$nextTick(() => {
                this.$refs.saveDialog.open("Show").setData(row);
            });
        },
        //设置状态
        async setStatus(row) {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    row.status == 0 ? "menu/disabled" : "menu/enable",
                    { menuIds: [row.menuId] }
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                    this.$refs.mytable.getData();
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        //删除
        async opDelete(row) {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "menu/delete",
                    { supplierIds: [row.supplierId] }
                );
                if (returnValue.status == 200) {
                    this.$message.success("操作成功");
                    this.$refs.mytable.getData();
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        //批量删除
        async opBatchDelete() {
            // eslint-disable-next-line no-debugger
            //debugger;
            this.$confirm(
                `确定删除选中的 ${this.selectionRows.length} 项吗？`,
                "提示",
                {
                    type: "warning",
                }
            )
                .then(async () => {
                    const menuIds = this.selectionRows.map(
                        (p) => p.menuId
                    );
                    try {
                        var returnValue =
                            await this.$API.auth.PostProcessApi.post(
                                "menu/delete",
                                { menuIds: [...menuIds] }
                            );
                        if (returnValue.status == 200) {
                            this.$message.success("操作成功");
                            this.$refs.mytable.getData();
                        } else {
                            this.$message.error("操作失败:" + returnValue.msg);
                        }
                    } catch (error) {
                        this.loading = false;
                        return false;
                    }
                })
                .catch(() => {});
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selectionRows = selection;
        },
        async handleClick(tab) {
            this.queryList = { orgMenuType: tab };
            await this.$nextTick();
            await this.opBindData(); 
        },
        async opBindData() {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "menu/trees",
                    {...this.queryList}
                );
                if (returnValue.status == 200) {
                    this.menuTree=returnValue.response;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        resetSearch() {
            this.queryList = { orgMenuType: this.queryList.orgMenuType };
        },
        async initOptions() {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "base-dictionary/options",
                    { Codes: ["Status"] }
                );
                if (returnValue.status == 200) {
                    this.optionsState = returnValue.response.find(
                        (p) => p.code == "Status"
                    ).optionsDetails;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
    },
    async mounted() {
        await this.initOptions();
        await this.opBindData();
    },
};
</script>
